<?php
	$username = "root";
	$password = "papamama";
	$hostname = "phamthi.cs.hut.fi";
	
	$dbhandle = mysql_connect($hostname, $username, $password) or die ("Unable to connect to MySQL");	
	$db = mysql_select_db("new_Restaurant", $dbhandle) or die("Could not select the database");
	
	//check if it is online booking or order the food in the restaurant
	//if it is food order in the restaurant, then check the sequence number
	$book_online = false;
	$isValid = true;
	
	if(isset($_GET['submit'])){
		if(isset($_GET[v])) {
			processOnlineBooking();
		}
		else {
			processSubmission();
		}	
	}
	else{
			
	}
	
	function checkURL() {
		if (!isset($_GET['v'])){
			$book_online = true;		
		}
		else {
			$v = $_GET['v'];
			//extract the value from v
			$hash = substr($v, 0, 20);
			$table = substr($v, 20, strlen($v) - 20);
			$result = mysql_query("select seq from Taps where tableNum = ".$table);
			$row = mysql_fetch_array($result);
	
			//calculate the hash value of seq number
			$hash_seq = sha1($row{'seq'} + 1, TRUE);
						
			//compare the sequence number
			if(strcmp($hash_seq, $hash) == 0) {
			//return the html file
				include('menu.html');
				//insert an entry in the database with committed = 'N';
				$next_seq = (int)$row{'seq'} + 1;
				date_default_timezone_set("Europe/Helsinki");
				$date = date('d/m/Y h:i:s', time());
				$query = "insert into Taps values('$table', '$next_seq', '12345678901234567890', '$date', '0000', 'N')";
				//echo $query;
				$result = mysql_query($query);
			}
			else{
				echo "Sequence is not correct";			
			}
		}
	}
	
	//process online booking
	function processOnlineBooking(){
			
	}
	//process food submission
	function processSubmission() {	
	}
		if(isset($_GET['submit'])){
		$table = $_GET['table'];
		$total = $_GET['total'];		
		$v = $_GET['v'];
		$hash = substr($v, 0, 20);
		
		if($table == 0 or $total == 0) {
				echo "Please specify your table number and choose some food";
		}
		else{
			
			$choices = "";
			if(isset($_GET['raumuong'])){
				$choices = $choices.'rau muong - ';			
			}
			if(isset($_GET['ocluoc'])){
				$choices.='oc luoc - ';				
			}	
			
			if(isset($_GET['ocbungchuoidau'])){
				$choices.='oc bung chuoi dau - ';				
			}
			
			if(isset($_GET['bunca'])){
				$choices.='bun ca - ';				
			}
			
			if ($conn) {
				//insert the record to the database
				$query = "update Taps set commited = 'N' where table = '$table' and hash_seq like '$hash'";
				mysql_query($query);
				
				echo "The total money is $".$total."<br>";
			}
		}
	}
}
?>